<?php
$deId = $_GET['pid'] > 0 ? $_GET['pid'] : $_POST['pid'];

if ($deId < 1 || $deId == '') {

    header('Location: dienst_list.php');
    exit;
}
error_reporting(E_ALL);
include_once('../controller/includes/db_access.php');

function transforme_HTML($chaine, $longueur = null) {
// Aide à empêcher les attaques XSS
// Supression des espaces inutiles.
    $chaine = trim($chaine);
// Empêche des problèmes potentiels avec le codec Unicode.
    $chaine = utf8_decode($chaine);
// HTMLise les caractères spécifiques à HTML.
    $chaine = htmlentities($chaine, ENT_NOQUOTES);
    $chaine = str_replace("#", "&#35;", $chaine);
    $chaine = str_replace("%", "&#37;", $chaine);
    $longueur = intval($longueur);
    if ($longueur > 0) {
        
        $chaine = substr($chaine, 0, $longueur);
    
        
    }
    return $chaine;
}



if ((isset($_POST['action']) ? $_POST['action'] : (isset($_GET['action']) ? $_GET['action'] : '')) == 'multiple') {

    $dienst = transforme_HTML($_POST['naam']);
    $descr = transforme_HTML($_POST['descr']);
    $vp = ($_POST['vp']);
    $prijs = transforme_HTML($_POST['prijs']);
    $tp = ($_POST['tp']);
    $hvd =($_POST['hvd']);
    $aktief =($_POST['aktief']);


// Insert a new user into the `user` table  
    $q_updateDienst = new Query;
    $q_updateDienst->update('diensten')
            ->set( array(
                'naam' => $dienst,
                'descr' => $descr,
                'eenxdienst' => $vp,
                'prijs' => $prijs,
                'altijd_toepassen' => $tp,
                'aantal_per_dag' => $hvd,
                'aktief' => $aktief
                    ))
            ->where_equal_to(
                    array('dienst_id' => $deId)
            
    );

    $resultDienstUpdate = $q_updateDienst->run();
    $dienst_id = $q_updateDienst->get_affected();
    if ($resultDienstUpdate) {

        //$Error = '<font color="green">successful : categorie is toegevoed</font>';
               header('Location: dienst_list.php');
    } else {

        $Error = '<font color="red">Error: Dienst is niet toegevoed</font>';
  
           
    }
}




if (isset($_GET['pid'])) {

    $id = $_GET['pid'];
    $q_DienstSelect = new Query;
    $q_DienstSelect
            ->select('*'
            )
            ->from('`diensten`')
            ->where_equal_to(
                    array(
                        '`diensten`.`dienst_id`' => $id,
                    )
    );

    $result_DienstSelect = $q_DienstSelect->run();

    if (!($result_DienstSelect > 0)) {
        $Error = 'No dienst found.' . "\n";
    } else {
        $rows_dienst = mysql_fetch_object($result_DienstSelect);
    }
}
?>


<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<!--[if lt IE 9]>
<script
src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link rel="stylesheet" href="styles/style.css" type="text/css" />
<!--[if lte IE 7]>
<link rel="stylesheet" href="style_ie.css" />
<![endif]-->
<title>dbme!!!!</title>
</head>
<body>
<div id="bloc_page">
<header>
 <a href="#"><img src="../images/dBmelogo.png" alt="referentie" alt="dbme!" /></a>
</header>  
 <nav>
    <?php include_once('header.php'); ?>
</nav>
 <section>
  <aside>
  <!--right side baar -->  
  </aside>
     
  <article>  
<table border="0" cellspacing="5" cellpadding="5">

        <tbody>
        <form name="form1" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
        <input type="hidden" name="action" value="multiple" />
        <input type="hidden" name="pid" value="<?php echo $_GET['pid']; ?>" />
            <tr>
                <td colspan="2"><h1>Dienst add</h1><br/><br/>
                <?php if(isset($Error)) echo $Error; ?></td>
            </tr>
            <tr>
                <td valign="top">Dienst naam:
                </td>
                <td valign="top">
                    <input type="text" name="naam" value="<?php echo $rows_dienst->naam; ?>" size="20" required="required" />
                   
                </td>
            </tr>
            <tr>
                <td valign="top">Beschrijving:<br />
                
                </td>
                <td valign="top">
                    <textarea name="descr" rows="6" cols="40" ><?php echo $rows_dienst->descr; ?></textarea>
                   
                </td>
            </tr>
            <tr>
                <td valign="top" colspan="2">
                <input type="radio" name="vp" value="Y" required="required" <?php echo $rows_dienst->eenxdienst == 'Y' ? ' checked':''?> /> Deze dienst 1 x per verhuur periode berekenen (bijv op/afbouw, alleen eerste en laaste dag) <br />
                <input type="radio" name="vp" value="N" <?php echo $rows_dienst->eenxdienst == 'N' ? ' checked':''?> /> Deze dienst elke dag van de verhuur periode berekenen (bijv technicus)
                </td>
            </tr>
             <tr>
                 <td valign="top">Prijs&nbsp;(incl. BTW):
                </td>
                <td valign="top">
                    
                    <input type="number" min="0"  max="999" step="0.01" name="prijs" value="<?php echo $rows_dienst->prijs; ?>" size="4" required="required"  />
                   
                </td>
            </tr>
             <tr>
                <td valign="top" colspan="2">
                <input type="radio" name="tp" value="N" required="required" <?php echo $rows_dienst->altijd_toepassen == 'N' ? ' checked': ''?> /> Deze dienst is naar keuze van klant wel/niet af te nemen. maximaal leveren<input type="number"  min="0" max="999" name="hvd" value="<?php echo $rows_dienst->aantal_per_dag; ?>" size="4" required="required" /><br />
                <input type="radio" name="tp" value="Y" <?php echo $rows_dienst->altijd_toepassen == 'Y' ? ' checked': ''?>/> Deze dienst wordt standaard toegepast op elke offerte.
                </td>
            </tr>
                 
             <tr>
                 
                <td valign="top"  colspan="2"><!--Hoe vaak per dag kunt u deze dienst maximaal leveren?<input type="number"  min="0" max="999" name="hvd" value="<?php //echo $rows_dienst->aantal_per_dag; ?>" size="4" required="required" />-->
                </td>
       
             
            </tr>
            <tr>
                <td valign="top">Aktief 
                                                                                    
             </td>
            <td valign="top">
              <input name="aktief" type="radio"  id="aktief" value="Y" <?php if ($rows_dienst->aktief == 'Y') echo ' checked'; ?> /> Ja 
              <input name="aktief" type="radio"  id="aktief" value="N" <?php if ($rows_dienst->aktief != 'Y') echo ' checked'; ?> /> Nee                                                           
             </td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" value="save" name="save" />
                  </td>
            </tr>
          </form></tbody>
</table>
          </article>
 
    <aside>
  <!--right side baar -->
       
        
   </aside>
</section>
 
 <footer>  
<p>Lorem ipsum dolor sit amet...</p>
<p>Vivamus sed libero nec mauris pulvinar facilisis ut non sem...</p>
<p>Phasellus ligula massa, congue ac vulputate non, dignissim at augue...</p>
</footer>
</div>
</body>
</html>